package com.atech.ebayadmin.category.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.atech.ebayadmin.category.dao.ICategoryDAO;
import com.atech.ebayadmin.category.model.Category;
import com.atech.ebayadmin.category.service.ICategoryService;
import com.atech.ebayadmin.inter.service.EbayService;
import com.ebay.soap.eBLBaseComponents.CategoryType;

public class CategoryService implements ICategoryService{
	private final static org.apache.commons.logging.Log logger = 
		org.apache.commons.logging.LogFactory.getLog(CategoryService.class);
    private ICategoryDAO categoryDAO;

	public ICategoryDAO getCategoryDAO() {
		return categoryDAO;
	}
	public void setCategoryDAO(ICategoryDAO categoryDAO) {
		this.categoryDAO = categoryDAO;
	}
	public Category getCategory(String id) throws Exception{
		logger.info("进入service业务方法");
		return categoryDAO.getCategory(id);
	}
	public List<Category> listCategoryByParentId(String parentId)throws Exception{
    	return categoryDAO.listCategoryByParentId(parentId);
    }
	public String[] getCategoryHierarchy(String id)throws Exception{
		String[] categoryInfoArray = new String[2];
		List<Category> tempList = new ArrayList<Category>();
		Category po = categoryDAO.getCategory(id);
		tempList.add(po);
		while( ! po.getParentId().equals("0")){
			po = categoryDAO.getCategory(po.getParentId());
			tempList.add(po);
		}
		StringBuilder idBuf = new StringBuilder(30);
		StringBuilder nameBuf = new StringBuilder(100);
		for(int i = tempList.size();i>0;i--){
			idBuf.append(tempList.get(i-1).getId());
			nameBuf.append(tempList.get(i-1).getName());
			if(i != 1){
				idBuf.append(",");
				nameBuf.append("-->");
			}
		}
		categoryInfoArray[0] = idBuf.toString();
		categoryInfoArray[1] = nameBuf.toString();
		return categoryInfoArray;
	}
	
	public List ascendToTop(String categoryId)throws Exception{
		List<Category> pathTemp = new ArrayList();
		String id = "";
		String parentId = categoryId;
		while(!id.equals(parentId))
		{
			id = parentId;
			System.out.println("id = " + id);
			Category cat = this.getCategory(id);
			pathTemp.add(cat);
			
			if(cat.equals(null))// added by proby, if cat returned null, indicate error by return input 
				return null;
			parentId = cat.getParentId();
			System.out.println("parentId = " + parentId);
		}
		
		List path = new ArrayList();
		System.out.println("size = " + pathTemp.size());
		for(int i = pathTemp.size()-1; i >= 0; i-- )
		{
			path.add(pathTemp.get(i));
		}
		
		return path;
	}
	public void getCategoriesFromEbay()throws Exception{
		// create a new service
		EbayService ebayService = new EbayService();
		// additional infor excepty the Api Context is needed ??? hellen for disscussion
		final CategoryType[] Categories=ebayService.getCategoriesCall("AgAAAA**AQAAAA**aAAAAA**ZXWXSg**nY+sHZ2PrBmdj6wVnY+sEZ2PrA2dj6wJnY+lCpmBpwydj6x9nY+seQ**ASsBAA**AAMAAA**ZjCxtOhvgbxm91q1msFgnsN056rRGb4VR34371qeqHcBDPsuW2qZJqQnMnxI1F1+CcZYXKwclCTIHwDtD/nMoIVkVydo3ecar3Y7PmkV5YE3B1rjzzkpZjxyTbbrqIiR2TlP7uQSEvtEYLJ6cr3BWRgjKXQN9z0jcTtoR5YMcSb83SaNM1ej1E3Nkz4LtR/4K9fdKe9Yy37pEfw39sj9sjQMaeQQ2K5ETOzsNOth4roEi3P9ZyrxsGDKCgXnWIBoFDXOEsbV52FUSHLfla7MXns7xhzSRvWr8mzwKPbI58qEdZ1z23pO5goWD2jObgr/Mma5+Rd6nwUjNPo9+/mSpMslYknaOiPHOF0eC2YV1yJcoPSqiFA639kJMIli5SkFn/3okcHzDPFhN8MOUfyvnpGPHU7fA24PW5NvYX6/wOG3SeOH5aE1O6Ev/IUGSYuIT9UirTSTS5lMoLMd3Ih/klVbPUsZapw6QW9xEt7yeHAFRkPyNRmlYfFtipSUNddJJ0XJF6DbFW5JAAT0Nt1SfbXDJYIE7/rzOnzR4Qp0Nc9SXK/A9CneabVOvlnyju5NwcX9RLk8Ls6v+nz0Z8tYXGw1llwf1GBBHGJRTlR70odzOJgF4bYMG4aLt2JdZqgdG4c28MK0H/m4YTVbSrt4ewFXCFRd5x7z67zxgyAp13IwwXMEFL1nMKa29ufEO6vQNye7vr3RzA+qPrgjL4TN9PVDyro1BUzcWXpV6LUbAl5XlGJF+KkxDsVtltJdCYvO");
		if (Categories == null)
		{
			return;
		}
		// the following code needs recoded according to the Categories Struct
		for (int row=0; row<Categories.length; row++)
		{
			CategoryType cat = Categories[row];
			Category po = new Category();
			// local DB table for categories contains four columns: 
			// ID, Name, ParentID, IsLeaf				
			po.setId(cat.getCategoryID());
			po.setName(cat.getCategoryName());
			po.setParentId(cat.getCategoryParentID()[0]);
			po.setIsLeaf(cat.getCategoryParentID()!=null);// IsLeaf? 
			//save to DB, should save function be overloaded here?
			categoryDAO.saveCategory(po);   //save - overloaded DAO API
			/**/				
		}
	}
}
	
